/**
 * 拖动事件绑定插件
 * @author Ly
 * @version 1.0
 */
/**
 * 拖动事件绑定，一个元素只能绑定一次事件
 * @param {Object} config 对拖动操作的约束
 * config = {
	 callBack : function() {} // 回调函数
 }
 */
Ly.BindDrag = function(config /* Object */) {
	var _this = this;
	
	config = config || {};
	
	this.config = {
		range     : config.range || {},
		target    : config.target || document.body,
		callBack  : config.callBack || function() {},
		moveEvent : config.moveEvent || function() {},
		objs      : []
	};
	this.private = {
		initialization : function(obj /* Object */) {
			var config = _this.config,
				handler = {
				objMouseDown : function(o, event) {
					this.style.cursor = 'move';
					
					event = window.event || event;
					
					this._x = (event.layerX || event.clientX);
					this._y = (event.layerY || event.clientY);
					
					if (this.setCapture) {
						this.setCapture();
					} else if (window.captureEvents) {
						window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
					}
					
					Ly.addEvent('onmousemove', handler.documentMouseMove, document);
					Ly.addEvent('onmouseup', handler.documentMouseUp, document);
				},
				documentMouseMove : function(o, event) {
					var x, y;
					
					event = window.event || event;
					
					x = (event.pageX || event.clientX) - Ly.getX(config.target);
					y = (event.pageY || event.clientY) - Ly.getY(config.target);
					 !+"\v1"? document.selection.empty() : window.getSelection().removeAllRanges();
					obj.style.left = (x - obj._x) + 'px';
					obj.style.top  = (y - obj._y) + 'px';
					config.moveEvent(event);
				},
				documentMouseUp : function(o, event) {
					event = window.event || event;
					
					if (obj.releaseCapture) {
						obj.releaseCapture();
					} else if (window.releaseEvents) {
						window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
					}
					
					obj.style.cursor = 'default';
					
					Ly.removeEvent(document, 'onmousemove', handler.documentMouseMove);
					Ly.removeEvent(document, 'onmouseup', handler.documentMouseUp);
					config.callBack(event);
				}
			};
			
			if (obj.innerHTML == '') { obj.innerHTML == '&nbsp;' }
			
			Ly.addEvent('onmousedown', handler.objMouseDown, obj);
			
			config.objs.push({
				obj    : obj,
				events : handler
			});
		}
	};
};

Ly.BindDrag.prototype = {
	version : '1.0',
	/**
	 * 绑定事件
	 * @param {Object} obj 要绑定拖动事件的元素
	 */
	bind : function(obj /* Object */) {
		this.private.initialization(obj);
		return this;
	},
	/**
	 * 移除绑定的拖动事件
	 * @param {Object} obj 要移除事件的元素，省略则移除所有的元素
	 */
	remove : function(obj /* Object */) {
	},
	/**
	 * 给绑定拖动事件的元素添加更多的响应事件
	 * @param {Object} obj 要添加响应事件的元素
	 * @param {Function} fun 要响应的事件
	 */
	addEvent : function(obj /* Object */, fun /* Function */) {
	},
	/**
	 * 移除元素的一个事件
	 * @param {Object} obj 要移动事件的元素
	 * @param {Function} fun 要移除的事件
	 */
	removeEvent : function(obj /* Object */, fun /* Function */) {
	}
};